package org.aaron.demos;

import org.aaron.demos.db.SqliteDatabaseHelper;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SqliteActivity extends Activity {

	private Button createdatabase_btn, updatedatabase_btn, dropdatabase_btn, insert_btn, update_btn, query_btn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.sqlitelayout);
		initActivity();
	}

	private void initActivity() {
		createdatabase_btn = (Button) this.findViewById(R.id.createdatabase_btn);
		dropdatabase_btn = (Button) this.findViewById(R.id.dropdatabase_btn);
		updatedatabase_btn = (Button) this.findViewById(R.id.updatedatabase_btn);
		insert_btn = (Button) this.findViewById(R.id.insert_btn);
		update_btn = (Button) this.findViewById(R.id.update_btn);
		query_btn = (Button) this.findViewById(R.id.query_btn);
		createdatabase_btn.setOnClickListener(new ButtonClickListener());
		dropdatabase_btn.setOnClickListener(new ButtonClickListener());
		updatedatabase_btn.setOnClickListener(new ButtonClickListener());
		insert_btn.setOnClickListener(new ButtonClickListener());
		update_btn.setOnClickListener(new ButtonClickListener());
		query_btn.setOnClickListener(new ButtonClickListener());
	}

	class ButtonClickListener implements OnClickListener {
		SqliteDatabaseHelper databaseHelper;
		SQLiteDatabase sqLiteDatabase;
		ContentValues values;
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.createdatabase_btn:
				// 创建demos_db数据库
				databaseHelper = new SqliteDatabaseHelper(SqliteActivity.this, "demos_db");
				databaseHelper.getReadableDatabase();
				break;
			case R.id.updatedatabase_btn:
				// 更新demos_db数据库版本
				databaseHelper = new SqliteDatabaseHelper(SqliteActivity.this, "demos_db", 2);
				databaseHelper.getReadableDatabase();
				break;
			case R.id.dropdatabase_btn:
				// 删除demos_db数据库
				databaseHelper = new SqliteDatabaseHelper(SqliteActivity.this, "demos_db");
				// deleteDatabase方法，在16 API中SQLiteDatabase 新添加了deleteDatabase方法
				boolean result = databaseHelper.deleteDatabase(SqliteActivity.this, "demos_db");
				System.out.println("delete database :" + result);
				break;
			case R.id.insert_btn:
				// 创建demos_db数据库
				databaseHelper = new SqliteDatabaseHelper(SqliteActivity.this, "demos_db");
				sqLiteDatabase = databaseHelper.getWritableDatabase();
				values= new ContentValues();
				values.put("id", 1);
				values.put("name", "aaron");
				long rowid=sqLiteDatabase.insert("user", null, values);
				Log.d("Insert RowId", String.valueOf(rowid));
				break;
			case R.id.update_btn:
				// 更新数据
				databaseHelper = new SqliteDatabaseHelper(SqliteActivity.this, "demos_db");
				sqLiteDatabase = databaseHelper.getWritableDatabase();
				values = new ContentValues();
				values.put("name", "fiona");
				int count=sqLiteDatabase.update("user", values, "id=?", new String[]{"1"});
				Log.d("Update RowCount", String.valueOf(count));
				break;
			case R.id.query_btn:
				// 查询数据
				databaseHelper = new SqliteDatabaseHelper(SqliteActivity.this, "demos_db");
				sqLiteDatabase = databaseHelper.getReadableDatabase();
				Cursor cursor=sqLiteDatabase.query("user", new String[]{"id","name"}, "id=?",  new String[]{"1"}, null, null, null);
				while(cursor.moveToNext()){
					Log.d("User ID:",String.valueOf(cursor.getInt(cursor.getColumnIndex("id"))));
					Log.d("User NAME:",cursor.getString(cursor.getColumnIndex("name")));
				}
				break;
			default:
				break;
			}

		}

	}
}
